<?php

namespace app\jiyun\controller\order;

use app\common\model\admin\AccountModel;
use app\common\model\collect\OrderExpressGoodsModel;
use app\common\model\collect\OrderExpressModel;
use app\common\model\jiyun\base\ChannelModel;
use app\common\model\jiyun\OrderModel;
use app\common\model\jiyun\OrderModel as JyOrderModel;
use app\common\model\jiyun\OrderPackingModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use app\common\model\shop\GoodsModel;
use app\common\model\shop\OrderInfoModel;
use app\common\model\shop\PlatformModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 集运单
 * @class Order
 * @package app\jiyun\controller\order
 * @auth shirukun
 * @date 2024/11/21 16:32
 */
class Order extends Controller
{
    // 不过滤user_id
    public $check_user = false;

    /**
     * 集运单列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        OrderModel::mQuery()->layTable(function (){
            $this->title = lang('集运单列表');

            // 公司列表
            if ($this->super) {
                $this->account_list = AccountModel::mk()->where(['status'=> 1])->column('title', 'id');
            }else{
                $this->account_list = [];
            }
            // 状态
            $this->status = OrderModel::status();
            // 平台列表
            $this->platform_list = PlatformModel::mk()->where(['status' => 1])->column('title', 'id');

        },function (QueryHelper $query){

            $query->order('id desc');
            $query->like("order_no")->equal("account_id,platform_id,status");

        });
    }

    protected function _index_page_filter(&$data){
        if ($this->super) {
            // 公司列表
            $account_list = AccountModel::mk()->where(['status' => 1])->column('title', 'id');
            // 仓库列表
            $warehouse_list = WarehouseModel::mk()->where(['status' => 1])->column('warehouse_c_name', 'id');
        }else{
            // 公司列表
            $account_list = [];
            // 仓库列表
            $warehouse_list = WarehouseModel::mk()->where(['status' => 1,'account_id'=>$this->adminUser['account_id']])->column('warehouse_c_name', 'id');
        }
        // 平台列表
        $platform_list = PlatformModel::mk()->where(['status' => 1])->column('title', 'id');
        // 状态
        $status = OrderModel::status();

        foreach ($data as &$item) {
            $item['company_name'] = $account_list[$item['account_id']] ?? "-";
            $item['platform_name'] = $platform_list[$item['platform_id']] ?? "-";
            $item['warehouse_c_name'] = $warehouse_list[$item['warehouse_id']] ?? "-";
            $item['status'] = $status[$item['status']] ?? "-";
        }
    }

    /**
     * 添加仓库
     * @auth true
     */
    public function info()
    {
        $id = input('id');

        //订单详情
        $order = OrderModel::mk()->find($id)->toArray();
        // 平台订单平台名称 、、Ozone
        $order['platform_name'] = PlatformModel::mk()->where(['id' => $order['platform_id']])->value('title');
        // 用户平台名称
        $order['shop_account_name'] = AccountModel::mk()->where(['id' => $order['shop_account_id']])->value('title');
        // 仓库名称
        $order['warehouse_c_name'] = WarehouseModel::mk()->where(['id' => $order['warehouse_id']])->value('warehouse_c_name');
        // 发货渠道
        $order['channel_name'] = ChannelModel::mk()->where(['id' => $order['channel_id']])->value('title');
        // 订单状态
        $order['status_name'] = OrderModel::status()[$order['status']] ?? '';
//        halt($order);

        $this->vo = $order;

        //$this->other_order_no = $order['other_order_no'];

       /* $express_obj = OrderExpressModel::mk()->whereIn('order_no', explode(',',$logic_no))->column('express_no','id');

        $this->express_no = $express_obj->column('express_no','id');
        $express_no       = array_flip($this->express_no); // 反转键值对
        $this->express_no_id = reset($express_no ) ;*/


        $this->fetch();
    }

    /**
     * 订单详情
     * @author shirukun
     */
    public function infoList ()
    {
        $order_no = $this->request->get('order_no');
        $order_no = explode(',',$order_no);
        // 新需求  展示物流单
        $status_arr = OrderExpressModel::getStatus();
        $list = OrderExpressModel::mk()->whereIn('order_no', $order_no)->where('status', '>=', 0)->select();

        $list->each(function ($item) use ($status_arr) {
            $item->status_name = $status_arr[$item->status];
        });

        $this->success('成功', $list, 0);

        $express_id = $this->request->get('express_id');
        $list = OrderExpressGoodsModel::mk()->where('order_express_id', $express_id)->select();
        $this->success('成功', $list, 0);
        $order_id = input('order_id');

        $list = OrderInfoModel::mk()->where('order_id', $order_id)->select();

        // 获取订单主表
        $order_info = \app\common\model\shop\OrderModel::mk()->field('id,platform_id,shop_id,order_no,order_number')->where('id', $order_id)->find();
        // 获取平台信息
        list($goods_model, $sku_model, $attr_model, $image_model) = GoodsModel::instance( $order_info['platform_id']);

        $sku_ids_arr = !empty($list) ? array_column($list->toArray(), 'goods_nu') : [];
        $map = [
            ['goods_nu', 'in', $sku_ids_arr]
        ];
        $sku_list_data = $sku_model->where($map)->column('*', 'goods_nu');
        $goods_ids = array_column($sku_list_data, 'goods_id');
        $goods_data = [];
        if(!empty($goods_ids)){
            $map = [
                ['id', 'in', $goods_ids]
            ];
            $goods_data = $goods_model->where($map)->column('*', 'id');
        }

        foreach ($list as &$item) {
            $item->price_str     = $item->price.' '.$item->money_unit;
            $item->all_price_str    = round(($item->price * $item->num), 4).' '.$item->money_unit;
            $item->attr_title = !empty($sku_list_data[$item['goods_nu']]['attribute_titles']) ? $sku_list_data[$item['goods_nu']]['attribute_titles'] : '';
            $item->goods_url = !empty($goods_data[$sku_list_data[$item['goods_nu']]['goods_id']]['source_url']) ? $goods_data[$sku_list_data[$item['goods_nu']]['goods_id']]['source_url'] : '';
            $item->goods_spu = !empty($goods_data[$sku_list_data[$item['goods_nu']]['goods_id']]['spu_code']) ? $goods_data[$sku_list_data[$item['goods_nu']]['goods_id']]['spu_code'] : '';
            $item->main_pic = !empty($sku_list_data[$item['goods_nu']]['image']) ? $sku_list_data[$item['goods_nu']]['image'] : '';
        }

        $this->success('成功', $list, 0);
    }

    /**
     * 表单前置回调
     */
    protected function _form_filter(array &$data)
    {

    }

    public function goodsList ()
    {

        OrderExpressGoodsModel::mQuery()->layTable(function (){

            $this->order_express_id = $this->request->get('order_express_id');
        },function (QueryHelper $query){

            $query->equal("order_express_id");

        });
    /*    $express_id = $this->request->get('express_id');
        $list = OrderExpressGoodsModel::mk()->where('order_express_id', $express_id)->select();
        $this->success('成功', $list, 0);*/

    }
    /**
     * 打印拣货单
     * @auth true
     */
    public function print_pick()
    {
        $ids = input('ids');

        $order = OrderModel::mk()->where([['id','in',$ids]])->with(['orderexpress'])->order('id desc')->select()->toArray();
//        halt($order);

        foreach ($order as &$v){
            $v['print_name'] = $this->adminUser['username'];
            $v['print_time'] = date('Y-m-d H:i:s');
            $v['user_address'] = $v['user_district'] . ' ' . $v['user_city'] . ' ' . $v['user_region'] . ' ' . $v['country'] . '<br/>' . $v['user_address'];
            $v['goods_total_num'] = 0;
            $v['order_info_num'] = 0;

            foreach ($v['orderexpress'] as $kK => &$vv){
                $v['order_info_num']++;
                $v['goods_total_num'] ++;
                $vv['no'] = ($kK+1);
                $vv['num'] = 1;
            }
        }
//        halt($order);

        $this->success('获取成功',[
            'jsonData'=>$order
        ]);
    }

    /**
     * 打印拣货码
     * @auth true
     */
    public function print_pick_code()
    {
        $id = input('id');

        $order = OrderModel::mk()->find($id);
        $order_packing = OrderPackingModel::mk()->where([['jiyun_order_id','=',$id]])->select();

        foreach ($order_packing as &$v)
        {
            $v['order_no'] = $order['order_no'];
            $v['print_time'] = date('Y-m-d H:i');
        }

        $this->success('获取成功',[
            'jsonData'=>$order_packing
        ]);
    }
}
